package com.soccer.soccerclub.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.soccer.soccerclub.entity.User;
import com.soccer.soccerclub.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName LoginController
 * @Description //TODO
 * @Author GuXinYu
 * @Date 2021/4/19 0:35
 * @Version 1.0
 **/
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public R login(String username, String password){
        if (StringUtils.checkValNull(username) || StringUtils.checkValNull(password)){
            return R.failed("用户名密码不能为空！");
        }
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (ObjectUtils.isEmpty(user)){
            return R.failed("用户名或密码错误");
        }
        String userPassword = user.getPassword();
        user.setPassword("");
        return R.ok(user);
    }

    /**
     * 管理员登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/adminlogin")
    public R adminlogin(String username, String password){
        if (StringUtils.checkValNull(username) || StringUtils.checkValNull(password)){
            return R.failed("用户名密码不能为空！");
        }
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (ObjectUtils.isEmpty(user)){
            return R.failed("用户名或密码错误");
        }
        if (user.getRole()!=1){
            return R.failed("权限不足");
        }
        String userPassword = user.getPassword();
        user.setPassword("");
        return R.ok(user);
    }
}
